package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.RegionInfoMapper;
import cc.rengu.igas.bomp.common.entity.RegionInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 行政区划代码信息表操作接口实现类
 * Created by 王成 on 2020/3/10
 */
public class RegionInfoMapperImpl implements RegionInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertRegionInfo(RegionInfo regionInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBompRegionInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, regionInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记行政区划代码信息失败,regionCode:<{}>,regionName:<{}>,id:<{}>,iReturnCode:<{}>",
                    regionInfo.getRegionCode(), regionInfo.getRegionName(), regionInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateRegionInfoById(RegionInfo regionInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompRegionInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, regionInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新行政区划代码信息失败,regionCode:<{}>,regionName:<{}>,id:<{}>,iReturnCode:<{}>",
                    regionInfo.getRegionCode(), regionInfo.getRegionName(), regionInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public RegionInfo selectRegionInfoById(String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompRegionInfoById");
        RegionInfo regionInfo = new RegionInfo();
        regionInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, regionInfo, regionInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行政区划代码信息失败,id:<{}>,iReturnCode:<{}>", id, iReturnCode);
            return null;
        }
        return regionInfo;
    }

    @Override
    public RegionInfo selectRegionInfoByPrimaryKey(String regionCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompRegionInfoByPrimaryKey");
        RegionInfo regionInfo = new RegionInfo();
        regionInfo.setRegionCode(regionCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, regionInfo, regionInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行政区划代码信息失败,regionCode:<{}>,iReturnCode:<{}>", regionCode, iReturnCode);
            return null;
        }
        return regionInfo;
    }

    @Override
    public List<RegionInfo> selectRegionInfoByParentRegionCode(String parentRegionCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompRegionInfoByParentRegionCode");
        RegionInfo regionInfo = new RegionInfo();
        regionInfo.setParentCode(parentRegionCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, regionInfo, RegionInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取行政区划代码信息失败,parentRegionCode:<{}>", parentRegionCode);
            return null;
        }
        return objectList.stream().map(item -> (RegionInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<RegionInfo> selectRegionInfoListByCondition(String parentRegionCode, String regionLevel, String regionName) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompRegionInfoListByCondition");
        RegionInfo regionInfo = new RegionInfo();
        regionInfo.setParentCode(parentRegionCode);
        regionInfo.setRegionLevel(regionLevel);
        regionInfo.setRegionName(regionName);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, regionInfo, RegionInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取行政区划代码信息失败或为空,parentRegionCode:<{}>,regionLevel:{},regionName:{}", parentRegionCode, regionLevel, regionName);
            return null;
        }
        return objectList.stream().map(item -> (RegionInfo) item).collect(Collectors.toList());
    }


    @Override
    public PageResult selectRegionInfoPageListByCondition(String parentRegionCode, String regionLevel, String regionName, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompRegionInfoListByCondition");
        RegionInfo regionInfo = new RegionInfo();
        regionInfo.setParentCode(parentRegionCode);
        regionInfo.setRegionLevel(regionLevel);
        regionInfo.setRegionName(regionName);
        return dbsUtil.dbsFetchPageListData(sqlStr, regionInfo, RegionInfo.class, pageInfo);
    }
}
